﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vivasky.T4Helper.Entities
{
	public class Table
	{

		public List<Column> Columns;
		public List<FKTable> FKTables;
		public string Name;
		public string CleanName;
		public string ClassName;
		public string PrimaryKey;
		public string Schema;
		public string QueryableName;

		public bool HasLogicalDelete()
		{
			return this.Columns.Any(x => x.Name.ToLower() == "deleted" || x.Name.ToLower() == "isdeleted");
		}
		public Column DeleteColumn
		{
			get
			{
				Column result = null;
				if (this.Columns.Any(x => x.Name.ToLower() == "deleted"))
					result = this.Columns.Single(x => x.Name.ToLower() == "deleted");
				if (this.Columns.Any(x => x.Name.ToLower() == "isdeleted"))
					result = this.Columns.Single(x => x.Name.ToLower() == "isdeleted");
				return result;
			}
		}
		public Column PK
		{
			get
			{
				return this.Columns.SingleOrDefault(x => x.IsPK) ?? this.Columns[0];
			}
		}
		public Column Descriptor
		{
			get
			{
				if (this.Columns.Count == 1)
				{
					return this.Columns[0];
				}
				else
				{
					//get the first string column
					Column result = null;
					result = this.Columns.FirstOrDefault(x => x.SysType.ToLower().Trim() == "string");
					if (result == null)
						result = this.Columns[1];
					return result;
				}
			}
		}
	}
}
